package com.sanxiau.dormitory.yang.service.impl;

import com.sanxiau.dormitory.yang.entity.*;
import com.sanxiau.dormitory.yang.face.FaceDetect;
import com.sanxiau.dormitory.yang.face.FaceSearch;
import com.sanxiau.dormitory.yang.mapper.AttendanceMapper;
import com.sanxiau.dormitory.yang.mapper.DoroutMapper;
import com.sanxiau.dormitory.yang.mapper.LeaveMapper;
import com.sanxiau.dormitory.yang.mapper.LoginMapper;
import com.sanxiau.dormitory.yang.service.DoroutService;
import com.sanxiau.dormitory.yang.service.LeaveService;
import com.sanxiau.dormitory.yang.util.GetAfterDay;
import com.sanxiau.dormitory.yang.vo.ResultInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class LeaveServiceImpl implements LeaveService {
    @Autowired
    LeaveMapper leaveMapper;
    @Autowired
    AttendanceMapper attendanceMapper;
    @Autowired
    LoginMapper loginMapper;
    @Override
    public ResultInfo addLeave(Leave leave,String leavetime,HttpSession session) {
        //System.out.println("leave-------------:"+leave);
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setMessage("");
        Student student1 = attendanceMapper.queryStudentByStu_id(session.getAttribute("userName")+"");
        //System.out.println("student1-----------:"+student1);
        if (leave.getStu_name().equals(student1.getStu_name()) && leave.getStu_id().equals(student1.getStu_id()) && leave.getHou_id().equals(student1.getHou_id()) && leave.getDor_id().equals(student1.getDor_id())){
            String start_date = leavetime.substring(0,16);
            String end_date = leavetime.substring(19,leavetime.length());
            Date now  = new Date();
            leave.setDate(now);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                leave.setStart_date(sdf.parse(start_date+":00"));
                leave.setEnd_date(sdf.parse(end_date+":00"));
                leave.setStu_study(student1.getStu_study());
                leave.setStu_class(student1.getStu_class());
                leave.setStu_cname(student1.getStu_cname());
                int num = leaveMapper.addLeave(leave);
                if (num > 0){
                    resultInfo.setMessage("申请提交成功，请等待");
                }else {
                    System.out.println("请假信息提交失败");
                    resultInfo.setMessage("请假信息提交失败");
                }
            } catch (ParseException e) {
                System.out.println("请假信息添加失败");
                resultInfo.setMessage("请假信息提交失败");
                e.printStackTrace();
            }
        }else {
            resultInfo.setMessage("请填写正确的信息");
        }
        return resultInfo;
    }

    @Override
    public List<Leave> queryLeavesByStu_id(HttpSession session) {
        LoginIn loginIn = attendanceMapper.queryUserByUser_name(session.getAttribute("userName")+"");
        List<Leave> leaves = new ArrayList<>();
        if (null != loginIn){
            if ("学生".equals(loginIn.getRemark())){
                leaves = leaveMapper.queryLeavesByStu_id(loginIn.getUser_name());
            }else {
                leaves = leaveMapper.queryAllLeaves();
            }
        }
        return leaves;
    }

    @Override
    public ResultInfo passLeaves(int[] ids, HttpSession session) {
        LoginIn loginIn = attendanceMapper.queryUserByUser_name(session.getAttribute("userName")+"");
        int count = 0;
        ResultInfo resultInfo = new ResultInfo();
        for (int i = 0; i < ids.length; i++) {
            Leave leave = leaveMapper.queryLeaveByID(ids[i]);
            if (null != leave){
                int num = leaveMapper.updateLeave(new Date(),loginIn.getTrue_name(),ids[i]);
                if (num>0){
                    count++;
                }
            }
        }
        if (count == ids.length){
            resultInfo.setMessage("已批准"+count+"条请假申请");
        }else {
            resultInfo.setMessage("发生错误，请重试");
        }
        return resultInfo;
    }
    @Override
    public ResultInfo overLeaves(int[] ids) {
        int count = 0;
        ResultInfo resultInfo = new ResultInfo();
        for (int i = 0; i < ids.length; i++) {
            Leave leave = leaveMapper.queryLeaveByID(ids[i]);
            if (null != leave){
                int num = leaveMapper.updateOverLeave(new Date(),ids[i]);
                if (num>0){
                    count++;
                }
            }
        }
        if (count == ids.length){
            resultInfo.setMessage("已销假"+count+"条");
        }else {
            resultInfo.setMessage("发生错误，请重试");
        }
        return resultInfo;
    }

    @Override
    public ResultInfo auth(String base64,HttpSession session) {
        ResultInfo resultInfo = new ResultInfo();
        JSONObject json0 = null;
        int userid = -1;
        String jpg1 = FaceDetect.faceDetect(base64);
        try {
            json0 = new JSONObject(jpg1);
            int error_code1 = json0.getInt("error_code");
            if (error_code1 > 0) {
                resultInfo.setMessage("认证失败，请重试!");
                System.out.println("Detect采集失败,请重试");
            } else {
                String jpg = FaceSearch.faceSearch(base64);
                JSONObject json1 = new JSONObject(jpg);
                int error_code = json1.getInt("error_code");
                if (error_code > 0) {
                    resultInfo.setMessage("认证失败，请重试!");
                    System.out.println("验证失败,请重试");
                } else {
                    JSONObject json2 = (JSONObject) json1.get("result");
                    JSONArray user_list = json2.getJSONArray("user_list");
                    JSONObject user = (JSONObject) user_list.get(0);
                    String user_id = user.getString("user_id");
                    userid = Integer.parseInt(user_id);
                    resultInfo.setId(userid);
                }
            }
            LoginIn loginIn = loginMapper.queryUserByID(userid);
            if (null != loginIn) {
                if (null != (session.getAttribute("userName")+"") && "" != (session.getAttribute("userName")+"") && (session.getAttribute("userName")+"").equals(loginIn.getUser_name())){
                    resultInfo.setMessage("认证成功!");
                }
            }else {
                resultInfo.setMessage("认证失败，请重试!");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }finally {
            return resultInfo;
        }
    }
}
